package com.ct.controller.goods;

import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.goods.model.po.GoodsAddPO;
import com.ct.application.goods.model.po.GoodsQueryPO;
import com.ct.application.goods.model.po.GoodsSeckillUpdatePO;
import com.ct.application.goods.model.po.GoodsUpdatePO;
import com.ct.application.goods.model.vo.GoodsSeckillVO;
import com.ct.application.goods.model.vo.GoodsVO;
import com.ct.application.goods.service.GoodsSeckillService;
import com.ct.application.goods.service.GoodsService;
import com.ct.application.goods.service.IAdminGoodsService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.ApiLog;
import com.ct.commons.annotation.HasAdmin;
import com.ct.commons.annotation.PermissionApi;
import com.ct.commons.aspect.limiter.AccessIpLimiter;
import com.ct.repository.commons.PageUtils;
import com.ct.utils.threadlocal.UserThreadLocal;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后台管理-商品管理
 */
@RestController
@PermissionApi(title = "商品管理模块",path="/api/v1/goods",code = "A1000",sorted = 1)
public class AdminGoodsController extends BaseController {

    @Resource
    private IAdminGoodsService adminGoodsService;
    @Resource
    private GoodsSeckillService goodsSeckillService;
    @Resource
    private GoodsService goodsService;

    /**
     * 商品列表
     * @return
     */
    @PostMapping("/goods/list")
    @PermissionApi(title = "商品列表",path="/api/v1/goods/list",code = "A1001",sorted = 1)
    public List<Object> list(){
        return adminGoodsService.findGoodsList();
    }

    /**
     * 商品添加
     * @return
     */
    @PostMapping("/goods/save")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "商品添加",path="/api/v1/goods/save",code = "A1002",sorted = 2)
    public boolean save(){
        System.out.println(UserThreadLocal.get().getId());
        return adminGoodsService.saveGoods();
    }

    /**
     * 获取商品列表数据
     */
    @PostMapping("/goods/getGoodsList")
    @PermissionApi(title = "获取商品列表数据",path="/api/v1/goods/getGoodsList",code = "A1003",sorted = 3)
    public PageUtils<GoodsVO> getGoodsList(@Valid @RequestBody GoodsQueryPO queryPO) {
        return goodsService.getGoodsList(queryPO);
    }


    /**
     * 根据id查询商品
     */
    @PostMapping("/goods/get/{id}")
    @PermissionApi(title = "根据id查询商品",path="/api/v1/goods/get/{id}",code = "A1004",sorted = 4)
    public GoodsVO getGoodsById(@PathVariable("id") Long id) {
        return goodsService.getGoodsById(id);
    }


    /**
     * 新增商品
     */
    @PostMapping("/goods/add")
    @HasAdmin
    @ApiLog("新增商品")
    @AccessIpLimiter
    @PermissionApi(title = "新增商品",path="/api/v1/goods/add",code = "A1005",sorted = 5)
    public Boolean add(@Valid @RequestBody GoodsAddPO po) {
        return goodsService.add(po);
    }


    /**
     * 设置商品为秒杀商品
     */
    @PostMapping("/goods/toSeckillGoods/{id}")
    @HasAdmin
    @ApiLog("设置商品为秒杀商品")
    @AccessIpLimiter
    @PermissionApi(title = "设置秒杀商品",path="/api/v1/goods/toSeckillGoods/{id}",code = "A1006",sorted = 6)
    public Boolean add(@PathVariable("id") Long id) {
        return goodsSeckillService.add(id);
    }


    /**
     * 修改商品
     */
    @PostMapping("/goods/update")
    @HasAdmin
    @ApiLog("修改商品")
    @AccessIpLimiter
    @PermissionApi(title = "修改商品",path="/api/v1/goods/update",code = "A1007",sorted = 7)
    public Boolean update(@RequestBody GoodsUpdatePO po) {
        return goodsService.update(po);
    }


    /**
     * 删除商品
     */
    @PostMapping("/goods/delete/{id}")
    @HasAdmin
    @ApiLog("删除商品")
    @AccessIpLimiter
    @PermissionApi(title = "删除商品",path="/api/v1/goods/delete/{id}",code = "A1008",sorted = 8)
    public Boolean delete(@PathVariable("id") Long id) {
        return goodsService.delete(id);
    }


    /**
     * 获取秒杀商品列表数据
     */
    @PostMapping("/seckillGoods/getSeckillGoodsList")
    @PermissionApi(title = "查询秒杀商品",path="/api/v1/seckillGoods/getSeckillGoodsList",code = "A1009",sorted = 9)
    public PageUtils<GoodsSeckillVO> getSeckillGoodsList(@Valid @RequestBody GoodsQueryPO queryPO) {
        return goodsSeckillService.getSeckillGoodsList(queryPO);
    }


    /**
     * 根据id查询秒杀商品
     */
    @PostMapping("/seckillGoods/get/{id}")
    @PermissionApi(title = "根据id查询秒杀商品",path="/api/v1/seckillGoods/get/{id}",code = "A1010",sorted = 10)
    public GoodsSeckillVO getSeckillGoodsById(@PathVariable("id") Long id) {
        return goodsSeckillService.getSeckillGoodsById(id);
    }


    /**
     * 删除秒杀商品
     */
    @PostMapping("/seckillGoods/delete/{id}")
    @HasAdmin
    @ApiLog("删除秒杀商品")
    @AccessIpLimiter
    @PermissionApi(title = "删除秒杀商品",path="/api/v1/seckillGoods/delete/{id}",code = "A1011",sorted = 11)
    public Boolean deleteSeckillGoods(@PathVariable("id") Long id) {
        return goodsSeckillService.delete(id);
    }


    /**
     * 修改秒杀商品
     */
    @PostMapping("/seckillGoods/update")
    @HasAdmin
    @ApiLog("修改秒杀商品")
    @AccessIpLimiter
    @PermissionApi(title = "修改秒杀商品",path="/api/v1/seckillGoods/update",code = "A1012",sorted = 12)
    public Boolean update(@Valid @RequestBody GoodsSeckillUpdatePO po) {
        return goodsSeckillService.update(po);
    }

    @PostMapping("/goods/update/status")
    @HasAdmin
    @ApiLog("修改商品状态")
    @AccessIpLimiter
    @PermissionApi(title = "修改商品状态",path="/api/v1/goods/update/status",code = "A1013",sorted = 13)
    public Boolean updateStatus(@RequestBody StatusRequest statusRequest) {
        return goodsService.updateStatus(statusRequest);
    }

    @PostMapping("/seckillGoods/update/status")
    @HasAdmin
    @ApiLog("修改秒杀商品状态")
    @AccessIpLimiter
    @PermissionApi(title = "修改秒杀商品状态",path="/api/v1/seckillGoods/update/status",code = "A1013",sorted = 13)
    public Boolean updateStatusSeckillGoods(@RequestBody StatusRequest statusRequest) {
        return goodsSeckillService.updateStatus(statusRequest);
    }
}
